package ai.modules;

import util.ArrayList;
import util.Iterator;
import util.List;
import ai.AIConstant;
import ai.AIDebug;
import ai.AIUtil;
import ai.Trackable;
import ai.engine.AICity;
import ai.engine.AIResource;
import ai.engine.AIUnit;
import ai.engine.Task;
import ai.engine.factories.TaskFactory;
import engine.GameEngine;
import engine.data.UnitType;

public class AnalysisModule {

	public final static void gatherTasks() {
		// TaskFactory.removeAllTasks();
		ruleExplore();
		ruleAttackUnit();
		ruleProtectUnit();
		ruleBuildCity();
		//ruleProtectCity();
		ruleBuildUnit();
	}

	public static final void ruleExplore() {
		if (AIUtil.getCurrentAIPlayer().explorationPourcentage < AIUtil
				.getCurrentAIP().StopExploringPourcentage) {
			TaskFactory.getTask(TaskFactory.EXPLORE_TASK, null);
		}
	}

	public static final void ruleAttackUnit() {
		if (AIUtil.getCurrentAIPlayer().enemyUnits == null)
			return;
		System.out.println("ruleAttackUnit");
		Iterator it = AIUtil.getCurrentAIPlayer().enemyUnits.iterator();
		while (it.hasNext()) {
			TaskFactory.getTask(TaskFactory.ATTACK_UNIT_TASK, (Trackable) it
					.next());
		}
	}

	public static final void ruleProtectUnit() {
		if (GameEngine.currPlayer.units == null)
			return;
		Iterator it = GameEngine.currPlayer.units.iterator();
		AIUnit currentUnit;
		while (it.hasNext()) {
			currentUnit = (AIUnit) it.next();
			if (currentUnit.type == UnitType.DIPLOMAT)
				TaskFactory.getTask(TaskFactory.PROTECT_UNIT_TASK, currentUnit);
		}
	}

	public static final void ruleBuildCity() {
		if (AIUtil.getCurrentAIPlayer().cityNeeded) {
			Trackable buildingSpot = AIUtil.getBestBuildingSpot(
					GameEngine.currPlayer, GameEngine.map, (byte) 0);
			TaskFactory.getTask(TaskFactory.BUILD_CITY_TASK, buildingSpot);
		}
	}

	public static final void ruleProtectCity() {
		if (GameEngine.currPlayer.cities == null)
			return;
		Iterator it = GameEngine.currPlayer.cities.iterator();
		AICity currentCity;
		while (it.hasNext()) {
			currentCity = (AICity) it.next();
			if (currentCity.units != null
					&& currentCity.units.size() < AIUtil.getCurrentAIP().minFortifiedUnitsNeeded)
				TaskFactory.getTask(TaskFactory.PROTECT_CITY_TASK, currentCity);
		}
	}

	public final static void ruleBuildUnit() {
		if (AIUtil.getCurrentAIPlayer().tasks == null)
			return;
		Iterator itTasks = AIUtil.getCurrentAIPlayer().tasks.iterator();
		List newResources = new ArrayList();
		Task currentTask;
		AIResource currentResource;
		while (itTasks.hasNext()) {
			currentTask = (Task) itTasks.next();
			if (currentTask.status == AIConstant.STATUS_WAIT_4_RESOURCES) {
				List resourcesNeeded = currentTask
						.getAdditionalResourcesNeeded();
				if (resourcesNeeded != null) {
					Iterator itResources = resourcesNeeded.iterator();
					while (itResources.hasNext()) {
						currentResource = (AIResource) itResources.next();
						newResources.add(currentResource);
					}
				}
			}

		}
		Iterator itResources = newResources.iterator();
		while (itResources.hasNext()) {
			currentResource = (AIResource) itResources.next();
			TaskFactory.getTask(TaskFactory.BUILD_UNIT_TASK, currentResource);
		}

	}

}
